/**
 * ImageInformation
 * 
 * 20/11/2013
 * 
 * Copyright GPL
 */
package com.jtmproject.activities;

import java.io.FileNotFoundException;
import java.io.InputStream;

import com.jtmproject.classes.AnalyzeColorBitmap;
import com.jtmproject.classes.GetPreferences;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

/**
 * This class shows an activity to show the result of the image scanner
 * 
 * @author Javier Tejedor
 */
public class ImageInformation extends Activity implements OnClickListener {

	//random number generate for the programmer to identify the request code
	private static final int REQUEST_CODE_PICK_IMAGE = 777;

	private Button btnSelectImage;
	private ImageView imgImageSelected;
	private TextView txvRed;
	private TextView txvBlue;
	private TextView txvGreen;
	private LinearLayout linearRed;
	private LinearLayout linearBlue;
	private LinearLayout linearGreen;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_image_information);

		btnSelectImage = (Button) findViewById(R.id.btnSelectImage);
		btnSelectImage.setOnClickListener(this);
		
		imgImageSelected = (ImageView) findViewById(R.id.imgImageToAnalyze);
		
		txvRed = (TextView) findViewById(R.id.txvRed);
		txvBlue = (TextView) findViewById(R.id.txvBlue);
		txvGreen = (TextView) findViewById(R.id.txvGreen);

		linearRed = (LinearLayout) findViewById(R.id.linearRed);
		linearBlue = (LinearLayout) findViewById(R.id.linearBlue);
		linearGreen = (LinearLayout) findViewById(R.id.linearGreen);
		
		setVisiblesTextViews();
		
	}
	
	/**
	 * this sets the visibility of the textviews, depend of the configuration
	 */
	private void setVisiblesTextViews() {
		String color = 
				GetPreferences.getGetPreferences().getColorSelected(this);
		
		//with jdk 1.7 we will be able to do a "switch-case"
		
		if(color.equals("red")){
			
			linearBlue.setVisibility(View.INVISIBLE);
			linearGreen.setVisibility(View.INVISIBLE);
			linearRed.setVisibility(View.VISIBLE);
			
		} else if(color.equals("blue")){
			
			linearBlue.setVisibility(View.VISIBLE);
			linearGreen.setVisibility(View.INVISIBLE);
			linearRed.setVisibility(View.INVISIBLE);
			
		} else if(color.equals("green")){
			
			linearBlue.setVisibility(View.INVISIBLE);
			linearGreen.setVisibility(View.VISIBLE);
			linearRed.setVisibility(View.INVISIBLE);
			
		}
	}

	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnSelectImage:

			launchPhotoPickerIntent();

			break;

		default:
			break;
		}
	}

	/**
	 * this launch the gallery to pick an image
	 */
	private void launchPhotoPickerIntent() {
		Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
		photoPickerIntent.setType("image/*");
		startActivityForResult(photoPickerIntent, REQUEST_CODE_PICK_IMAGE);   
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(
	 * int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(
			int requestCode, int resultCode, Intent imageReturnedIntent) { 
		
		super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

		switch(requestCode) { 
		
		case REQUEST_CODE_PICK_IMAGE:
			if(resultCode == RESULT_OK){  
				
				try {
					Uri selectedImage = imageReturnedIntent.getData();
					
					InputStream imageStream = 
							getContentResolver().openInputStream(selectedImage);
					
					Bitmap imageChoosed = 
							BitmapFactory.decodeStream(imageStream); 

					imgImageSelected.setImageBitmap(imageChoosed);
					
					analyzeColor(imageChoosed);
					
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				}
			}
			break;
				
		}
	}
	
	/**
	 * this analyzes the color of the image
	 * @param bitmap
	 */
	private void analyzeColor(Bitmap bitmap) {
		AnalyzeColorBitmap analyzeColorBitmap = new AnalyzeColorBitmap(bitmap);
		txvRed.setText(
				String.valueOf(analyzeColorBitmap.getPercentageRed()) + "%");
		
		txvBlue.setText(
				String.valueOf(analyzeColorBitmap.getPercentageBlue()) + "%");
		
		txvGreen.setText(
				String.valueOf(analyzeColorBitmap.getPercentageGreen()) + "%");
	}
	
}
